package com.mygenostics.misc;

import java.util.Map;

import javax.faces.context.FacesContext;

public class WebUtil {
	
	public static String getCurrentViewId() {
		return FacesContext.getCurrentInstance().getViewRoot().getViewId();
	}
	
	public static Map<String, Object> getSessionMap() {
		return FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
	}
	
	public static String normalizeViewId(String url) {
		if(url != null) {
			int lastSlashPos = url.lastIndexOf("/");
			int lastDotPos = url.lastIndexOf(".");
			
			if(lastSlashPos >= 0 && lastDotPos >= 0) {
				return url.substring(lastSlashPos + 1, lastDotPos);
			}
			else if(lastDotPos >= 0) {
				return url.substring(0, lastDotPos);
			}
			else if(lastSlashPos >= 0) {
				return url.substring(lastSlashPos + 1);
			}
		}
		
		return url;
	}
}
